package site.qianli.discover.web.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.jta.UserTransactionAdapter;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import site.qianli.discover.common.dto.UserDto;
import site.qianli.discover.common.exception.user.UserCanNotBeNullException;
import site.qianli.discover.common.util.UUIDUtils;
import site.qianli.discover.common.vo.Article;
import site.qianli.discover.common.vo.User;
import site.qianli.discover.service.UserService;
import springfox.petstore.model.Pet;

@Controller
@RequestMapping("hello")
@Api(description = "helllllo")
public class HelloAction {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private UserService userService;

	@RequestMapping("insert")
	@ResponseBody
	@ApiOperation(value = "插入", httpMethod = "PUT")
	public ResponseEntity<String> insert(@RequestBody User user) {
		logger.info("add user:{}", JSON.toJSON(user));
		return new ResponseEntity<String>(userService.insert(user), HttpStatus.OK);
	}

	@RequestMapping("insertArray")
	@ResponseBody
	@ApiOperation(value = "插入", httpMethod = "PUT")
	public ResponseEntity<String> insertArray(@RequestBody List<User> userList) {
		logger.info("add user:{}", JSON.toJSON(userList));
		for (User user : userList) {
			userService.insert(user);
		}
		return new ResponseEntity<String>("OK", HttpStatus.OK);
	}

	/**
	 * restful风格
	 * @param id
	 * @return
	 */
	@RequestMapping("user/{id}")
	@ResponseBody
	@ApiOperation(value = "查询", httpMethod = "GET")
	public User user(@PathVariable("id") String id) {
		return userService.selectByPrimaryKey(id);
	}

	@RequestMapping("list")
	@ResponseBody
	@ApiOperation(value = "list", httpMethod = "GET")
	public List<User> list() {
		return userService.selectAll();
	}

	@RequestMapping("insertUserAndArticle")
	@ResponseBody
	@ApiOperation(value = "插入", httpMethod = "PUT")
	public ResponseEntity<Boolean> insertUserAndArticle(@RequestBody User user) {
		Article article = new Article();
		article.setContent("content");
		article.setUserId("userId");
		article.setTitle("title");
		return new ResponseEntity<Boolean>(userService.insertUserAndArtical(user, article ), HttpStatus.OK);
	}
}
